<template>
	<view class="container">
		<!-- 信息列表 -->
		<view class="content">
			<view v-if="infoListData != null && infoListData != ''">
				<view v-for="(item,index) in receInfoList" :key="index">
					<view class="sentTimes">
						<text v-if="dateYMDay == item.sentYMDay">今天</text>
						<text v-else-if="item.sentYMDay == datePrevTwoDay">昨天</text>
						<text v-else>{{item.sentYMDay}}</text>
					</view>
					<view v-for="(item_2,index) in item.sentInfo" :key="index" v-if="item_2.sentYMDay == item.sentYMDay"
						@click="getListInfoId(item_2)">
						<view class="info_pre">
							<view class="receiverName">
								来自{{item_2.sponsorName}}的消息:
							</view>
							<view class="contentTxt">
								{{item_2.content}}
							</view>
							<view class="sentTime">
								{{item_2.sentTime}}
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="whole" v-else>
				<image src="../../static/image/zanwushuju@2x.png" mode="aspectFit"></image>
				<text>~暂无结果~</text>
			</view>
		</view>
		<!-- 详情信息 -->
		<view class="infoListAll" v-show="isShow">
			<view class="listInfoStyle">
				<view class="infoImg">
					<image src="/static/image/Fill 1@2x.png" mode="center"></image>
				</view>
				<scroll-view class="contentTxtInfo">
					{{sponsorInfo.content}}
				</scroll-view>
				<view class="sponsorName">
					{{sponsorInfo.sponsorName}}
				</view>
			</view>
			<view class="imgCancels">
				<image @click="cancels" src="/static/image/Close@2x.png" mode="aspectFit"></image>
			</view>
		</view>

		<!-- 遮罩层 -->
		<view class="mask" v-show="isMask"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// title: '主动关怀',
				infoListData: [],
				receInfoTime: [],
				receInfoList: [],
				sponsorInfo: [],
				userId: '',
				isMask: false,
				isShow: false,
				dateYMDay: '',
				datePrevTwoDay: ''
			}
		},
		onLoad() {
			this.getDate();
			this.getUserId();
			this.getInfoList();
		},
		onPullDownRefresh() {
			setTimeout(() => {
				uni.stopPullDownRefresh();
				this.getInfoList();
			}, 1000);
		},
		methods: {
			async getInfoList() {
				uni.showLoading({
					title: "加载中",
					mask: true
				});
				const res = await this
					.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
						url: '/api-operate/activecare/byReceiver/' + this.userId,
						method: "get",
					})
				if (res.success == true) {
					setTimeout(() => {
						uni.hideLoading();
						this.infoListData = res.body.command;
						for (let i in this.infoListData) {
							this.infoListData[i].image = '/static/image/Noodle Bowl@2x.png';
							if (this.infoListData[i].sentDate != null) {
								var sentDates = this.infoListData[i].sentDate;
								var timearr = sentDates.replace(" ", ":").replace(/\:/g, "-").split("-");
								this.infoListData[i].sentDate = timearr[0] + "-" + timearr[1] + "-" + timearr[
										2] + " " +
									timearr[3] + ":" +
									timearr[4];
								this.infoListData[i].sentYMDay = timearr[0] + "-" + timearr[1] + "-" + timearr[
									2];
								this.infoListData[i].sentTime = timearr[3] + ":" + timearr[4];
							}
							if (this.receInfoTime.indexOf(this.infoListData[i].sentYMDay) == -1) {
								this.receInfoTime.push(this.infoListData[i].sentYMDay);
							}
						}
						let obj = Object.assign({}, this.receInfoTime);
						let resultArr = [];
						for (let key in obj) {
							let objs = new Object();
							objs.sentYMDay = obj[key];
							resultArr.push(objs);
						}
						for (let num in resultArr) {
							resultArr[num].sentInfo = [];
							for (let num2 in this.infoListData) {
								if (resultArr[num].sentYMDay == this.infoListData[num2].sentYMDay) {
									resultArr[num].sentInfo.push(this.infoListData[num2]);
								}
							}
						}
						this.receInfoList = resultArr;
					}, 100)
				}
			},
			//-------------------------------------------
			//获取当前时间对比发送时间
			getDate() {
				var date = new Date();
				var year = date.getFullYear(),
					month = date.getMonth() + 1,
					day = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate();
				month > 0 && month < 10 ? (month = "0" + month) : '';
				this.dateYMDay = year + "-" + month + "-" + day;
				var day1 = new Date();
				day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
				var prevDay = day1.getDate();
				var prevMonth = day1.getMonth() + 1;
				prevDay = prevDay < 10 ? ("0" + prevDay) : prevDay;
				prevMonth = prevMonth < 10 ? ("0" + prevMonth) : prevMonth;
				var s1 = day1.getFullYear() + "-" + prevMonth + "-" + prevDay;
				this.datePrevTwoDay = s1;
			},
			//-----------------------------------------------------
			//根据id查看详情信息
			async getListInfoId(item) {
				const res = await this
					.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
						url: '/api-operate/activecare/' + item.activecareId,
						method: "get",
					})
				if (res.success == true) {
					this.sponsorInfo = res.body.command;
					this.isMask = true;
					this.isShow = true;
				}
			},
			//---------------------------------------------------
			//取消按钮
			cancels() {
				this.isMask = false;
				this.isShow = false;
			},
			//------------------------------------------------
			//获取当前发送者的id
			getUserId() {
				this.userId = uni.getStorageSync('id');
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		height: 100%;

		.content {
			margin-top: 3%;

			.sentTimes {
				text {
					width: 100%;
					display: flex;
					justify-content: center;
					font-size: 56rpx;
					color: #000000;
				}
			}

			.info_pre {
				width: 90%;
				// height: 230rpx;
				margin: 4% auto;
				border: 1px solid #e1e1e1;
				box-shadow: 1px 1px 10px #9eaeb5;
				background-color: white;
				border-radius: 8px;
				position: relative;

				.receiverName {
					width: 97%;
					margin: 3% 0 3% 2%;
					font-size: 56rpx;
					color: #000000;
				}

				.contentTxt {
					width: 66%;
					margin: 3% 0 3% 5%;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
					font-size: 56rpx;
					color: #000000;
				}

				.sentTime {
					position: absolute;
					right: 0;
					bottom: 0;
					margin-right: 2%;
					font-size: 56rpx;
					color: #000000;
				}
			}

			.whole {
				text-align: center;
				font-size: 60rpx;
				color: #a6a6a6;
				display: flex;
				flex-direction: column;
				align-items: center;

				image {
					margin: 250rpx 0 50rpx 0;
					width: 100%;
				}

				text {
					width: 100%;
				}
			}
		}

		//----------------------------------------------
		//详情数据
		.infoListAll {
			position: fixed;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
			margin: auto auto;
			width: 86%;
			height: 700rpx;
			z-index: 99;

			//-----------------------------------------------
			//信息详情区
			.listInfoStyle {
				width: 100%;
				height: 600rpx;
				border-radius: 8rpx;
				background-color: #fff5ec;
				color: #000000;
				font-size: 56rpx;
				position: relative;

				.infoImg {
					width: 120rpx;
					height: 120rpx;
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					margin: -60rpx auto 0 auto;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.contentTxtInfo {
					width: 96%;
					height: 68%;
					padding: 12% 2% 0 2%;
				}

				.sponsorName {
					height: 20%;
					display: flex;
					align-items: center;
					justify-content: flex-end;
					padding-right: 8%;
				}
			}

			//-----------------------------------------------------
			//取消按钮
			.imgCancels {
				width: 100%;
				height: 200rpx;
				display: flex;
				justify-content: center;
				align-items: center;

				image {
					width: 100rpx;
					height: 100rpx;
				}
			}
		}

		//--------------------------------------------------------------------------------
		//遮罩层
		.mask {
			width: 100%;
			height: 100%;
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 10;
			background-color: #000;
			opacity: 0.6;
		}
	}
</style>
